package com.voice.server.controller;

import com.voice.server.authentication.TokenAuthentication;
import com.voice.server.entity.Function;
import com.voice.server.entity.User;
import com.voice.server.enums.UserTypeEnum;
import com.voice.server.input.LoginRequest;
import com.voice.server.input.RegisterRequest;
import com.voice.server.input.ResetPasswordRequest;
import com.voice.server.input.SendCodeRequest;
import com.voice.server.service.UserService;
import com.wangjiacai.utils.output.ResultVO;
import com.wangjiacai.utils.utils.EnumUtil;
import com.wangjiacai.utils.utils.ResultVOUtil;
import com.wangjiacai.utils.utils.TokenUtil;
import com.wangjiacai.utils.utils.ValidatorUtil;
import com.wangjiacai.utils.web.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/*
 * @auther: wangjiacai
 *
 * @create: 2019-01-27 19:19:41
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private RedisService redisService;

    @PostMapping("/login")
    public ResultVO postLogin(@Valid LoginRequest request, BindingResult bindingResult){
        ValidatorUtil.valid(bindingResult);
        User user=userService.login(request.getPhone(),request.getPassword());
        String token= TokenUtil.getUUID32();
        redisService.set(token,user.getId());
        TokenAuthentication tokenAuthentication=new TokenAuthentication();
        tokenAuthentication.addGrantedAuthority(EnumUtil.getMessageByCode(user.getUserType(), UserTypeEnum.class));
        tokenAuthentication.setCredentials(user.getId());
        redisService.set(token, tokenAuthentication);
        return ResultVOUtil.success((Object)token);
    }

    @PostMapping("/logout")
    public ResultVO postLogout(@RequestHeader("token")String token){
        redisService.delete(token);
        return ResultVOUtil.success();
    }

    @PostMapping("/register")
    public ResultVO postRegister(@Valid RegisterRequest registerRequest, BindingResult bindingResult){
        ValidatorUtil.valid(bindingResult);
        userService.registerRequest(registerRequest);
        return ResultVOUtil.success();
    }

    @GetMapping("/function")
    public ResultVO getUserFunction(@RequestHeader("token")String token){
        String userId= (String) redisService.get(token,TokenAuthentication.class).getCredentials();
        List<Function> list=userService.findFunctionById(userId);
        return ResultVOUtil.success(list);

    }

    @GetMapping("/info")
    public ResultVO getUserInfo(@RequestHeader("token")String token){
        String userId= (String) redisService.get(token,TokenAuthentication.class).getCredentials();
        User user=userService.findUserById(userId);
        return ResultVOUtil.success(user);
    }

    @PostMapping("/password")
    public ResultVO resetPassword(@Valid ResetPasswordRequest resetPasswordRequest, BindingResult bindingResult){
        ValidatorUtil.valid(bindingResult);
        userService.resetPassword(resetPasswordRequest);
        return ResultVOUtil.success();
    }

    @PostMapping("/send_code")
    public ResultVO postSendCode(@Valid SendCodeRequest sendCodeRequest, BindingResult bindingResult){
        ValidatorUtil.valid(bindingResult);
        userService.sendSmsCode(sendCodeRequest);
        return ResultVOUtil.success();
    }

}
